package cn.qihang.ssm.controller;

import cn.qihang.ssm.pojo.Member;
import cn.qihang.ssm.pojo.Orders;
import cn.qihang.ssm.pojo.Product;
import cn.qihang.ssm.pojo.Traveller;
import cn.qihang.ssm.service.MemberService;
import cn.qihang.ssm.service.OrdersService;
import cn.qihang.ssm.service.ProductService;
import cn.qihang.ssm.service.TravellerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: qihang
 * @Date: 2022/11/1 14:36
 * @Desc:
 */
@Controller
@RequestMapping("order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private ProductService productService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private TravellerService travellerService;
    /**
     * 查询所有订单
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("findAll")
    public ModelAndView findAll(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                @RequestParam(value = "size",defaultValue = "5") Integer size){

        PageHelper.startPage(page,size);
        List<Orders> orders = this.ordersService.findAll(page,size);
        PageInfo pageInfo = new PageInfo(orders);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.setViewName("order-list");
        return modelAndView;
    }

    /**
     * 订单详情
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public ModelAndView findById(@RequestParam(name = "id") Integer id){
        ModelAndView modelAndView = new ModelAndView();
        Orders order = this.ordersService.findById(id);
        modelAndView.addObject("order",order);
        modelAndView.setViewName("order-show");
        return modelAndView;
    }

    /**
     * 新增订单- 查询出 moember会员   product产品  traveller游客
     * @return
     */
    @RequestMapping("add")
    public ModelAndView add(){

        //查询product产品
        List<Product> productList = this.productService.findAll();

        //查询moember信息
        List<Member> memberList = this.memberService.findAll();

        List<Traveller> travellerList = this.travellerService.findAll();
        ModelAndView mv = new ModelAndView();
        mv.addObject("productList",productList);
        mv.addObject("memberList",memberList);
        mv.addObject("travellerList",travellerList);

        mv.setViewName("order-add");
        return mv;
    }

    /**
     * 新增订单
     * @param order
     * @return
     */
    @RequestMapping("save")
    public String save(Orders order) {
        this.ordersService.save(order);
        return "redirect:findAll.do";
    }




    /**
     * 修改订单前的数据回显
     * @param id
     * @return
     */
    @RequestMapping("editById")
    public ModelAndView editById(@RequestParam(name = "id") Integer id){
        ModelAndView modelAndView = new ModelAndView();
        Orders order = ordersService.findById(id);
        //查询product产品
        List<Product> productList = this.productService.findAll();

        //查询moember产品
        List<Member> memberList = this.memberService.findAll();
        modelAndView.addObject("productList",productList);
        modelAndView.addObject("memberList",memberList);
        modelAndView.addObject("order",order);
        modelAndView.setViewName("order-edit");
        return modelAndView;
    }


    /**
     * 修改订单
     * @return
     */
    @RequestMapping("edit")
    public String edit(Orders orders) {
        this.ordersService.edit(orders);
        return "redirect:findAll.do";
    }


    /**
     * 单个删除订单
     * @param id
     * @return
     */
    @RequestMapping("deleteById")
    public String deleteById(@RequestParam("id") Integer id) {
        this.ordersService.deleteById(id);
        return "redirect:findAll.do";
    }

    /**
     * 批量删除
     * @param request
     * @return
     */
    @RequestMapping("selectDelete")
    public String selectDelete(HttpServletRequest request){
        String ids = request.getParameter("ids");

        String[] split = ids.split(",");
        for (String id : split) {
            this.ordersService.deleteById(Integer.parseInt(id));
        }
        return "redirect:findAll.do";
    }
}
